// Decompiled by:       Fernflower v0.8.6
// Date:                03.08.2014 10:26:35
// Copyright:           2008-2012, Stiver
// Home page:           http://www.neshkov.com/ac_decompiler.html

package com.dynamixsoftware.printershare.mdns;

import com.dynamixsoftware.printershare.mdns.DnsQuestion;
import com.dynamixsoftware.printershare.mdns.DnsRecord;
import com.dynamixsoftware.printershare.mdns.DnsRecordAddress;
import com.dynamixsoftware.printershare.mdns.DnsRecordPointer;
import com.dynamixsoftware.printershare.mdns.DnsRecordService;
import com.dynamixsoftware.printershare.mdns.DnsRecordText;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class DnsPacketIn {

   List<DnsRecord> answers;
   private byte[] data;
   int flags;
   // $FF: renamed from: id int
   int field_553;
   private int len;
   int numAdditionals;
   int numAnswers;
   int numAuthorities;
   int numQuestions;
   private int off;
   List<DnsQuestion> questions;
   long receivedTime;


   // $FF: renamed from: <init> (java.net.DatagramPacket) void
   public void DnsPacketIn(DatagramPacket var1) throws IOException {
      InetAddress var2 = var1.getAddress();
      this.data = var1.getData();
      this.len = var1.getLength();
      this.off = var1.getOffset();
      this.questions = Collections.emptyList();
      this.answers = Collections.emptyList();
      this.receivedTime = System.currentTimeMillis();
      this.field_553 = this.readUnsignedShort();
      this.flags = this.readUnsignedShort();
      this.numQuestions = this.readUnsignedShort();
      this.numAnswers = this.readUnsignedShort();
      this.numAuthorities = this.readUnsignedShort();
      this.numAdditionals = this.readUnsignedShort();
      if(this.numQuestions > 0) {
         this.questions = Collections.synchronizedList(new ArrayList(this.numQuestions));
         int var23 = 0;

         while(true) {
            int var24 = this.numQuestions;
            if(var23 >= var24) {
               break;
            }

            DnsQuestion var25 = new DnsQuestion(this.readName(), this.readUnsignedShort(), this.readUnsignedShort());
            this.questions.add(var25);
            ++var23;
         }
      }

      int var3 = this.numAnswers + this.numAuthorities + this.numAdditionals;
      if(var3 > 0) {
         this.answers = Collections.synchronizedList(new ArrayList(var3));

         for(int var4 = 0; var4 < var3; ++var4) {
            String var5 = this.readName();
            int var6 = this.readUnsignedShort();
            int var7 = this.readUnsignedShort();
            int var8 = this.readInt();
            int var9 = this.readUnsignedShort();
            int var10 = var9 + this.off;
            Object var11 = null;
            switch(var6) {
            case 1:
            case 28:
               DnsRecordAddress var29 = new DnsRecordAddress(var5, var6, var7, var8, this.readBytes(this.off, var9));
               var11 = var29;
               break;
            case 5:
            case 12:
               String var20 = "";

               label51: {
                  String var22;
                  try {
                     var22 = this.readName();
                  } catch (IOException var27) {
                     var27.printStackTrace();
                     break label51;
                  }

                  var20 = var22;
               }

               DnsRecordPointer var28 = new DnsRecordPointer(var5, var6, var7, var8, var20);
               var11 = var28;
            case 13:
            default:
               break;
            case 16:
               byte[] var19 = this.readBytes(this.off, var9);
               DnsRecordText var31 = new DnsRecordText(var5, var6, var7, var8, var19);
               var11 = var31;
               break;
            case 33:
               int var12 = this.readUnsignedShort();
               int var13 = this.readUnsignedShort();
               int var14 = this.readUnsignedShort();
               String var15 = "";

               label46: {
                  String var18;
                  try {
                     var18 = this.readName();
                  } catch (IOException var26) {
                     var26.printStackTrace();
                     break label46;
                  }

                  var15 = var18;
               }

               DnsRecordService var30 = new DnsRecordService(var5, var6, var7, var8, var12, var13, var14, var15);
               var11 = var30;
            }

            if(var11 != null) {
               ((DnsRecord)var11).setRecordSource(var2);
               this.answers.add((DnsRecord) var11);
            } else if(this.answers.size() < this.numAnswers) {
               this.numAnswers += -1;
            } else if(this.answers.size() < this.numAnswers + this.numAuthorities) {
               this.numAuthorities += -1;
            } else if(this.answers.size() < this.numAnswers + this.numAuthorities + this.numAdditionals) {
               this.numAdditionals += -1;
            }

            this.off = var10;
         }
      }

   }

   private int get(int var1) throws IOException {
      if(var1 >= 0 && var1 < this.len) {
         return 255 & this.data[var1];
      } else {
         throw new IOException("parser error: offset=" + var1);
      }
   }

   private byte[] readBytes(int var1, int var2) throws IOException {
      byte[] var3 = new byte[var2];
      System.arraycopy(this.data, var1, var3, 0, var2);
      return var3;
   }

   private int readInt() throws IOException {
      return (this.readUnsignedShort() << 16) + this.readUnsignedShort();
   }

   private String readName() throws IOException {
      StringBuffer var1 = new StringBuffer();
      int var2 = this.off;
      int var3 = -1;
      int var4 = var2;

      while(true) {
         int var5 = var2 + 1;
         int var6 = this.get(var2);
         if(var6 == 0) {
            if(var3 < 0) {
               var3 = var5;
            }

            this.off = var3;
            return var1.toString();
         }

         switch(var6 & 192) {
         case 0:
            this.readUTF(var1, var5, var6);
            var2 = var5 + var6;
            var1.append('.');
            break;
         case 192:
            if(var3 < 0) {
               var3 = var5 + 1;
            }

            int var7 = (var6 & 63) << 8;
            int var10000 = var5 + 1;
            var2 = var7 | this.get(var5);
            if(var2 >= var4) {
               throw new IOException("bad domain name: possible circular name detected. name start: " + var4 + " bad offset: 0x" + Integer.toHexString(var2));
            }

            var4 = var2;
            break;
         default:
            var2 = var5;
         }
      }
   }

   private void readUTF(StringBuffer var1, int var2, int var3) throws IOException {
      var1.append(new String(this.readBytes(var2, var3), "UTF-8"));
   }

   private int readUnsignedShort() throws IOException {
      int var1 = this.off;
      this.off = var1 + 1;
      int var2 = this.get(var1) << 8;
      int var3 = this.off;
      this.off = var3 + 1;
      return var2 + this.get(var3);
   }

   void append(DnsPacketIn var1) {
      if(this.isQuery() && this.isTruncated() && var1.isQuery()) {
         if(var1.numQuestions > 0) {
            if(Collections.EMPTY_LIST.equals(this.questions)) {
               this.questions = Collections.synchronizedList(new ArrayList(var1.numQuestions));
            }

            this.questions.addAll(var1.questions);
            this.numQuestions += var1.numQuestions;
         }

         if(Collections.EMPTY_LIST.equals(this.answers)) {
            this.answers = Collections.synchronizedList(new ArrayList());
         }

         if(var1.numAnswers > 0) {
            this.answers.addAll(this.numAnswers, var1.answers.subList(0, var1.numAnswers));
            this.numAnswers += var1.numAnswers;
         }

         if(var1.numAuthorities > 0) {
            this.answers.addAll(this.numAnswers + this.numAuthorities, var1.answers.subList(var1.numAnswers, var1.numAnswers + var1.numAuthorities));
            this.numAuthorities += var1.numAuthorities;
         }

         if(var1.numAdditionals > 0) {
            this.answers.addAll(var1.answers.subList(var1.numAnswers + var1.numAuthorities, var1.numAnswers + var1.numAuthorities + var1.numAdditionals));
            this.numAdditionals += var1.numAdditionals;
         }

      } else {
         throw new IllegalArgumentException();
      }
   }

   public int elapseSinceArrival() {
      return (int)(System.currentTimeMillis() - this.receivedTime);
   }

   public DnsRecord getAnswer(int var1, String var2) {
      for(int var3 = 0; var3 < this.answers.size(); ++var3) {
         DnsRecord var4 = (DnsRecord)this.answers.get(var3);
         if(var4.type == var1 && var4.name.equals(var2)) {
            return var4;
         }
      }

      return null;
   }

   public List<DnsRecord> getAnswers() {
      return this.answers;
   }

   public List<DnsQuestion> getQuestions() {
      return this.questions;
   }

   public boolean isQuery() {
      return ('\u8000' & this.flags) == 0;
   }

   public boolean isResponse() {
      return ('\u8000' & this.flags) == '\u8000';
   }

   public boolean isTruncated() {
      return (512 & this.flags) != 0;
   }
}
